<template>
	<view>
		
		
		<l-file ref="lFile" @up-success="onSuccess"></l-file>
		
		<view class="padding text-center">

			<view class="padding">
				<button @tap="getFile">选择文件</button>
			</view>
			<view class="padding" style="font-size: 26rpx;text-align: center;">
				<text decode>{{tip}}</text>
			</view>
			
			<view 
			style="margin: 0rpx 20rpx 20rpx 20rpx;border-bottom: 1rpx #D9D9D9 solid;display: flex;flex: 10;text-align: center;padding: 20rpx;" 
			v-for="item in tempFilesSead">
				
		        <!-- <view style="flex:3">1</view> -->
				<view style="flex:8;text-align: left;">{{item.name}}</view>
				<view style="flex:2">{{item.size}}</view>
			</view>
			
			<view class="padding">
				<button @tap="getImage">上传</button>
			</view>
			
		</view>
	</view>
</template>

<script>
	import lFile from '@/components/l-file/l-file.vue'

	let _this;
	export default {
		components:{lFile},
		data() {
			return {
				localPath: '',
				tip: ``,
				superiorInfo:{},
				tempFilesSead:[], //已经选择的文件
				fileList:[] //已经上传的文件
			}
		},
		onLoad(option){
			_this=this;
			console.log(JSON.parse(option.obj))
			this.superiorInfo=JSON.parse(option.obj);
		},
		mounted() {  
		            
		}  ,
		methods: {
			getFile(){
				uni.chooseImage({
					count: 9, //默认9
					sizeType: ['original'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['camera','album'], //从相机、相册选择
					success: (res) => {
						var tempFilePaths = res.tempFiles;//tempFilePaths获取是undefined
						
						for (var i = 0; i < tempFilePaths.length; i++) {
							console.log(tempFilePaths[i])
							let ssize="";
							if(tempFilePaths[i].type=="text/plain"){
								ssize=((tempFilePaths[i].size*4)/1000).toFixed(2)+"kb";
							}else{
								ssize=(tempFilePaths[i].size/1000).toFixed(2)+"kb";
							}
							_this.tempFilesSead.push({
								name:tempFilePaths[i].name,
								size:ssize,
								type:tempFilePaths[i].type,
								path:tempFilePaths[i].path
							});
						}
							//console.log(_this.fileList);
					}
				});
			},
			getImage(){
				uni.showLoading({
				    title: '文件上传中...'
				});
			//	setTimeout(function(){
					let val= uni.getStorageSync("pathHost");
					let value=uni.getStorageSync("JSESSIONID");		
						let fileys="";
					for (var i = 0; i < _this.tempFilesSead.length; i++) {
						 uni.uploadFile({
							url: val + "/ggcapp/sys/file/webupload/upload"+";JSESSIONID="+value+"?__ajax=true&mobileLogin=true&uploadPath=/appFile/",
							filePath: _this.tempFilesSead[i].path,
							name: "file", 
							success(res) {
								let obj=JSON.parse(res.data);
								//console.log(obj)
								if(obj.success){
									_this.fileList.push({
										imgPath: obj.body.url
									})
									fileys=fileys+obj.body.url+"|";
								//	console.log(fileys)	
								}
							}
						 });  
					}
					
					setTimeout(function(){
						//console.log(fileys)
						let sp = fileys.substring(0,fileys.length-1);
						uni.request({
							url: val + "/ggcapp/infoupload/infoupload/ggInfoUpload/save"+";JSESSIONID="+value+"?__ajax=true&mobileLogin=true",
							data:{
								pointId:_this.superiorInfo.id,
							   filePath:sp
							},
							success: (data) => {
								console.log(data)
								uni.hideLoading();
								uni.showToast({
									title: data.data.msg,
									duration: 2000
								});
								setTimeout(function(){
									uni.navigateBack({
									    delta: 1
									});
									
								},1300);
							}
						})
					},500)
					
					
					
			//	},1000)
			},
			onSuccess(res) {
				console.log('上传成功回调',JSON.stringify(res));
				uni.showToast({
					title: JSON.stringify(res),
					icon: 'none'
				})
			}
		}
	}
</script>

<style>
.padding-sm {
	padding: 20upx;
}

.padding {
	padding: 15upx;
}
</style>
